/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.assistant.csp.web;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.assistant.common.exception.ImplementNotFoundException;
import org.assistant.common.utils.ClassUtils;
import org.assistant.common.utils.WebUtils;
import org.assistant.common.web.servlet.mvc.Controller;
import org.assistant.common.web.servlet.mvc.DataType;
import org.assistant.common.web.servlet.mvc.ResultData;
import org.assistant.csp.data.Menu;
import org.assistant.csp.domain.MenuDomain;

public class VmenuController extends Controller {
	private static final long serialVersionUID = -6743916108424971550L;
	private MenuDomain menuManager;

	private static String webRoot = null;

	public VmenuController() throws ImplementNotFoundException {
		menuManager = ClassUtils.getInterfaceImplement(MenuDomain.class);
	}

	protected Object requestDispatcher(HttpServletRequest request,
			HttpServletResponse response, String methodName) {
		if (webRoot == null) {
			webRoot = WebUtils.getWebRoot(request) + "/";
		}

		try {
			if ("getTallyMenuNodes".equals(methodName)) {
				return getTallyMenuNodes(request, response);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

	/**
	 * 在页面上输出菜单数据的JavsScript数组形式
	 * 
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	public ResultData<?> getTallyMenuNodes(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		List<Menu> menuList = null;

		String nodeid = request.getParameter("nodeid");
		if (nodeid == null || nodeid.trim().equals("")) {
			menuList = this.menuManager.getAllMenus();
		} else {
			menuList = new ArrayList<Menu>();
		}

		StringBuffer buff = getMenuNodes(menuList);

		ResultData<String> resultData = new ResultData<String>(
			buff.toString(), DataType.original_xml);

		return resultData;
	}

	/**
	 * 将菜单数据生成JavaScript数组的形式
	 * 
	 * @param menuList
	 * @return
	 */
	private StringBuffer getMenuNodes(List<Menu> menuList) {
		StringBuffer buff = new StringBuffer();
		buff.append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
		buff.append("<rows>");

		buff.append("<page>").append(1).append("</page>");
		buff.append("<total>").append(1).append("</total>");
		buff.append("<records>").append(1).append("</records>");

		for (int i = 0; i < menuList.size(); i++) {
			Menu menu = menuList.get(i);
			buff.append(getMenuNode(menu));// 添加子结点的数据
		}

		buff.append("</rows>");
		return buff;
	}

	/**
	 * 生成一个树形菜单的结点
	 * 
	 * @param menu
	 * @return
	 */
	private StringBuffer getMenuNode(Menu menu) {
		StringBuffer buff = new StringBuffer();
		buff.append("<row>");// 本结点开始

		buff.append("<cell>").append(menu.getId()).append("</cell>");
		buff.append("<cell>").append(menu.getName()).append("</cell>");
		if (null == menu.getUrl() || "".equals(menu.getUrl())) {
			buff.append("<cell/>");
		} else {
			buff.append("<cell>").append(webRoot + menu.getUrl()).append(
				"</cell>");
		}
		buff.append("<cell>").append(menu.getLevel()).append("</cell>");
		buff.append("<cell>").append(menu.getLft()).append("</cell>");
		buff.append("<cell>").append(menu.getRgt()).append("</cell>");

		boolean leaf = false;
		if (menu.getRgt() == (menu.getLft() + 1)) {
			leaf = true;
		} else {
			leaf = false;
		}
		buff.append("<cell>").append(leaf).append("</cell>");

		boolean expanded = leaf;
		buff.append("<cell>").append(expanded).append("</cell>");

		buff.append("</row>");// 本结点结束

		return buff;
	}
}
